<?php
require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'phpFlickr.php';

class PBNYFlickr{

  var $api_key = null;
  var $api_secret = null;
  var $api_token = null;
  
  var $myflickr = null;
  
  function __construct($ini_file){
    $result = parse_ini_file($ini_file);
    foreach(array('api_key', 'api_secret', 'api_token') as $k)
      $this->$k = $result[$k];
    
    if(!$this->check_attributes())
      throw new Exception("Invalid ini file");
    
    $this->myflickr = new phpFlickr($this->api_key, $this->api_secret);
    $this->myflickr->setToken($this->api_token);
  }
  
  function print_attributes(){
    print "KEY: $this->api_key \n";
    print "SECRET: $this->api_secret \n";
    print "TOKEN: $this->api_token";
  }
  
  function get_user($username){
    return $this->myflickr->people_findByUsername($username);
  }
  
  function get_photosets($user_id){
    return $this->myflickr->photosets_getList($user_id);
  }
  
  function upload_photo($photo, $title = null, $description = null){
    return $this->myflickr->sync_upload($photo, $title, $description);
  }
  
  # Returns a ticket to check status of uploaded photo
  function upload_photo_async($photo, $title = null, $description = null){
    return $this->myflickr->async_upload($photo, $title, $description);
  }
  
  function check_tickets($tickets){
    return $this->myflickr->photos_upload_checkTickets($tickets);
  }
  
  function move_photo_to_photoset($photo_id, $photoset_id){
    return $this->myflickr->photosets_addPhoto($photoset_id, $photo_id);
  }
  
  function get_photo_sizes($photo_id){
    return $this->myflickr->photos_getSizes($photo_id);
  }
  
  function buildPhotoURL($photo_id, $size = "Medium"){
    $result = $this->myflickr->photos_getInfo($photo_id);
    return $this->myflickr->buildPhotoURL($result['photo'], $size);
  }
  
  private function check_attributes(){
    return !in_array(null, array($this->api_key, $this->api_secret, $this->api_token));
  }
}

?>